package com.wu.flandre.component;

import com.wu.flandre.entity.common.EmailEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * EmailComponent <br/>
 *
 * @author 50816 <br/>
 * @date 2019/12/11 22:58 <br/>
 */
@Component
@Async("getThreadPoolExecutor")
@Slf4j
public class EmailComponent {

    @Value("${spring.mail.username}")
    private String sender;

    @Autowired
    JavaMailSender mailSender;

    public void sendLoginMail(EmailEntity entity) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(sender);
            message.setSubject(entity.getSubject());
            message.setTo(entity.getRecipient());
            message.setText(entity.getContent());
            mailSender.send(message);
        } catch (Exception e) {
            log.error("邮件发送失败,收件人:" + entity.getRecipient() + ",异常信息:" + e.getMessage());
        }
    }

}
